//
//  LPOrganizeReportChooseStreetViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/8/21.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPOrganizeReportChooseStreetViewController.h"
#import "LPSearchContentView.h"
#import "LPOrangizeStreetTableViewCell.h"
#define STREET @"STREET"
@interface LPOrganizeReportChooseStreetViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
/**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/**    */
@property (strong, nonatomic) UITextField  *searchTextField;

/**  搜索的文字  */
@property (strong, nonatomic) NSString  *keyWord;


@end

@implementation LPOrganizeReportChooseStreetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.hidden = YES;
    self.rightButton.hidden = NO;
    [self.rightButton setTitle:@"搜索" forState:UIControlStateNormal];
    [self layoutUI];
}

- (void)action_rightButton:(UIButton *)sender{
    
    [self getData];
}
#pragma mark - 数据
//下拉刷新
- (void)getData{
    
    if (self.keyWord.length == 0) {
        [YJProgressHUD showSuccess:@"请输入搜索街道" inview:self.view];
        return;
    }
    
  
    __weak typeof(self) weakSelf = self;
    NSDictionary *paramer   =
    @{@"SEARCH_DEPT_NAME": self.keyWord};
    [YJProgressHUD showMessage:@"搜索中..." duration:10 view:self.view];
    [NetWorkingManager postWithUrlString:@"pudong.org.action.ListOrgDept[searchAllParty]" Parameter:paramer Success:^(id object) {
        [YJProgressHUD hide];
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[LPOrangeStreetModel class] json:[object objectForKey:@"ORG_PARTYMEMBERS"]] mutableCopy];
        }
        [weakSelf.tableView reloadData];
        if (weakSelf.dataSouce.count == 0) {
            [LYEmptyManager showNoDataWithView:weakSelf.tableView];
        }
    } Fail:^(id object) {
         [YJProgressHUD hide];
        [weakSelf.dataSouce removeAllObjects];
        [weakSelf.tableView reloadData];
        [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
            [weakSelf getData];
        }];
        
        
    }];
    
    
}



#pragma mark - 界面
- (void)layoutUI{
    [self.navigView addSubview:self.searchTextField];
    [self.view addSubview:self.tableView];
    
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.keyWord = textField.text;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    self.keyWord = textField.text;
    [self getData];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length >= 50 && range.length!= 1 ){
        textField.text = [toBeString substringToIndex:50];
        return NO;
        
    }
    return YES;
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LPOrangeStreetModel *model = self.dataSouce[indexPath.row];
    CGFloat height  = [CalculateLabelHeight CalculateLabelHeightWithString:@"中英" font:16 * [UIScreen zj_textFontScale] width:[UIScreen zj_screenWith] - 30 * [UIScreen zj_screenWScale]].height;
    NSInteger zheng = model.HEIGHT / height;
    CGFloat lableHeiht = 0.0;
    if (zheng > 2) {
        lableHeiht = height * 2 + 5 * [UIScreen zj_screenHScale];
    }else{
        lableHeiht = model.HEIGHT;
    }

    return 30 * [UIScreen zj_screenHScale] + lableHeiht;;
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    LPOrangizeStreetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:STREET forIndexPath:indexPath];
    LPOrangeStreetModel *model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.block) {
        self.block(self.dataSouce[indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight , self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerClass:[LPOrangizeStreetTableViewCell class] forCellReuseIdentifier:STREET];
    }
    return _tableView;
}

- (UITextField *)searchTextField{
    if (!_searchTextField) {
        _searchTextField = ({
            UITextField *textField =  [[UITextField alloc] initWithFrame:CGRectMake(55 * [UIScreen zj_screenWScale], 28, [UIScreen zj_screenWith] - 110 * [UIScreen zj_screenWScale], 28)];
            textField.placeholder = @"输入关键字快速查找街道";
            textField.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
            textField.textColor = [UIColor textBlackColor];
            textField.textAlignment = NSTextAlignmentLeft;
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 25, 17)];
            image.image = [UIImage imageNamed:@"home_icon_search"];
            image.contentMode = UIViewContentModeCenter;
            textField.leftView = image;
            textField.leftViewMode = UITextFieldViewModeAlways;
            [textField setTextFieldRightforWidth:10 * [UIScreen zj_screenWScale]];
            textField.backgroundColor = [UIColor whiteColor];
            textField.layer.cornerRadius = 12;
            textField.layer.masksToBounds = YES;
            textField.delegate = self;
            textField.returnKeyType = UIReturnKeySearch;
            [textField becomeFirstResponder];
            textField;
        });
        
    }
    return _searchTextField;
    
}


- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}



@end
